<!DOCTYPE html>
<html manifest="converter.manifest">
<head><title>Unit Converter</title>
<link rel="apple-touch-icon" href="converter57.png">
<link rel="apple-touch-icon" sizes="114x114" href="converter114.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
<style>
body,input,a {margin:0; padding:0; background:#000; color:#fff; font:normal 20pt helvetica,arial; text-decoration:none}
input {border:none; background:#000; color:#fff; -webkit-appearance:none}
input,a {-webkit-tap-highlight-color:rgba(255,255,255,0.2)}
td {padding:0.1em 0.2em}
td:first-child {text-align:right}
td:first-child+td {color:#888}
td:first-child+td+td {text-align:left}
td:first-child+td+td+td b {font-weight:normal; color:#999}
em {font-style:normal; font-size:8pt; vertical-align:middle}
</style>
<body>
<input id="q" type="tel" placeholder="Converter" 
  onkeydown="switch(event.keyIdentifier){case'U+0023':case'U+002A':case'U+002B':case'U+002C':case'U+003B':event.preventDefault();this.value=this.value.substring(0,this.selectionStart)+'.'+this.value.substring(this.selectionEnd);};"
  onkeyup="calc();" onfocus="sel(this);"
  style="width:90%; margin:10px 5%; padding:2px; text-align:center">
<div id="output" onclick="$('q').focus();this.removeAttribute('onclick');"></div>
<div style="display:none">Offline Unit Converter for iPhone/iPod. Add to iPhone Home Screen for quick access.</div>
<script defer="defer">
function $(s) {return document.getElementById(s);}
var html=["<table cellspacing=0 border=0>"];
var arr=[
  ["cm", "inch", function(x){return x/2.54}],
  ["m", "ft", function(x){return x/(12*0.0254)}],
  ["m", "yds", function(x){return x/0.9144}],
  ["km", "mile", function(x){return x/0.0009144/1760}],
  ["inch", "cm", function(x){return x*2.54}],
  ["ft", "m", function(x){return x*12*0.0254}],
  ["yds", "m", function(x){return x*0.9144}],
  ["mile", "km", function(x){return x*0.0009144*1760}],
  ["kg", "lbs", function(x){return x*2.204622622}],
  ["lbs", "kg", function(x){return x/2.204622622}],
  ["litre", "gal<em>US</em>", function(x){return x*0.264172052}],
  ["gal", "litre", function(x){return x/0.264172052}],
  ["ft&sup2;", "m&sup2;", function(x){return x*144*0.0254*0.0254}],
  ["m&sup2;", "ft&sup2;", function(x){return x/(144*0.0254*0.0254)}],
  ["ft&sup3;", "m&sup3;", function(x){return x*1728*0.0254*0.0254*0.0254}],
  ["m&sup3;", "ft&sup3;", function(x){return x/(1728*0.0254*0.0254*0.0254)}],
  ["&#8451;", "&#8457;", function(x){return x*9/5 + 32;}],
  ["&#8457;", "&#8451;", function(x){return (x - 32)*5/9}]
];
for(var i=0; i<arr.length; i++) {
  html.push("<tr><td>"+arr[i][0]+"</td><td>&rArr;</td><td>"+arr[i][1]+"</td><td id='n"+i+"'><b>0</b></td></tr>");
}
$("output").innerHTML = html.join("") + "</table>";

function calc() {
  var n, val, str = parseFloat($("q").value.replace(/[^\d\-\.]/g, ""));
  for(var i=0; i<arr.length; i++) {
    val = isNaN(str) ? 0 : arr[i][2](str);
    n = val.toFixed(val>500?3:5).toString().replace(/\.?0+$/,"").replace(/(\.\d+)$/,"<b>$1</b>");
    $("n"+i).innerHTML = "<a href='copy:"+val+"' onclick='return!!0;'>"+n+"</a>";
  }
  if(window.localStorage) localStorage.lastInput = str;
}

function sel(d) {setTimeout(function() {d.selectionStart=0; d.selectionEnd=d.value.length;}, 0);}

if(window.localStorage && localStorage.lastInput) {
  $("q").value = localStorage.lastInput;
  calc();
}
</script>
</body>
</html>